package com.misty.encryption_decryption.base64;

import java.io.File;
import java.io.FileInputStream;
import java.util.Base64;

public class Base64Encoder {

    public static String encode(byte[] data) {
        String encodedString = Base64.getEncoder().encodeToString(data);
        return encodedString;
    }

    public static String encode(String data) {
        String encodedString = Base64.getEncoder().encodeToString(data.getBytes());
        return encodedString;
    }

    public static String encodeFile(String path) {
        String encodedString = null;
        File file = new File(path);
        FileInputStream inputFile = null;
        try {
            inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            encodedString = Base64.getEncoder().encodeToString(buffer);
            return encodedString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
